package com.shop.cereshop.business.controller.advice;


import com.shop.cereshop.commons.domain.common.Page;
import com.shop.cereshop.business.page.advice.BusinessAdviceVO;
import com.shop.cereshop.business.service.business.CereBusinessAdviceService;
import com.shop.cereshop.business.service.business.CereBusinessAdviceTrackService;
import com.shop.cereshop.commons.domain.business.CereBusinessAdvice;
import com.shop.cereshop.commons.domain.business.CereBusinessAdviceTrack;
import com.shop.cereshop.commons.domain.business.CerePlatformBusiness;
import com.shop.cereshop.commons.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 商家经营建议控制器
 */
@RestController
@RequestMapping("/advice")
@Api(value = "商家经营建议", tags = "商家经营建议")
public class BusinessAdviceController {

    @Autowired
    private CereBusinessAdviceService cereBusinessAdviceService;

    @Autowired
    private CereBusinessAdviceTrackService cereBusinessAdviceTrackService;

    @PostMapping("/add")
    @ApiOperation("添加经营建议")
    public Result<String> addAdvice(@RequestBody CereBusinessAdvice advice) {
        cereBusinessAdviceService.save(advice);
        return Result.success("添加成功");
    }

    @PutMapping("/update")
    @ApiOperation("更新经营建议")
    public Result<String> updateAdvice(@RequestBody CereBusinessAdvice advice) {
        cereBusinessAdviceService.update(advice);
        return Result.success("更新成功");
    }

    @DeleteMapping("/delete/{adviceId}")
    @ApiOperation("删除经营建议")
    public Result<String> deleteAdvice(@PathVariable Long adviceId) {
        cereBusinessAdviceService.delete(adviceId);
        return Result.success("删除成功");
    }

    @GetMapping("/get/{adviceId}")
    @ApiOperation("根据ID获取经营建议")
    public Result<CereBusinessAdvice> getAdviceById(@PathVariable Long adviceId) {
        CereBusinessAdvice advice = cereBusinessAdviceService.getById(adviceId);
        return Result.success(advice);
    }

    @PostMapping("/track/add")
    @ApiOperation("添加经营建议完成情况")
    public Result<String> addTrack(@RequestBody CereBusinessAdviceTrack track) {
        cereBusinessAdviceTrackService.save(track);
        return Result.success("添加成功");
    }

    @PutMapping("/track/update")
    @ApiOperation("更新经营建议完成情况")
    public Result<String> updateTrack(@RequestBody CereBusinessAdviceTrack track) {
        cereBusinessAdviceTrackService.update(track);
        return Result.success("更新成功");
    }

    @DeleteMapping("/track/delete/{trackId}")
    @ApiOperation("删除经营建议完成情况")
    public Result<String> deleteTrack(@PathVariable Long trackId) {
        cereBusinessAdviceTrackService.delete(trackId);
        return Result.success("删除成功");
    }

    @GetMapping("/track/get/{trackId}")
    @ApiOperation("根据ID获取经营建议完成情况")
    public Result<CereBusinessAdviceTrack> getTrackById(@PathVariable Long trackId) {
        CereBusinessAdviceTrack track = cereBusinessAdviceTrackService.getById(trackId);
        return Result.success(track);
    }

    @GetMapping("/track/getByAdviceAndShop")
    @ApiOperation("根据建议ID和商家ID获取完成情况")
    public Result<CereBusinessAdviceTrack> getTrackByAdviceAndShop(
            @RequestParam Long adviceId,
            @RequestParam Long shopId) {
        CereBusinessAdviceTrack track = cereBusinessAdviceTrackService.getByAdviceIdAndShopId(adviceId, shopId);
        return Result.success(track);
    }

    @GetMapping("/getById")
    @ApiOperation("根据商家ID获取经营建议列表")
    public Result<Page<BusinessAdviceVO>> getById(
            @RequestParam (defaultValue = "1") @ApiParam ("页码") Integer pageNum,
            @RequestParam(defaultValue = "10") @ApiParam ("每页数量") Integer pageSize,
            HttpServletRequest request) {
        //获取当前登录账户
        CerePlatformBusiness user = (CerePlatformBusiness) request.getAttribute("user");
        Page<BusinessAdviceVO> page = cereBusinessAdviceService.findByShopId(pageNum, pageSize, user.getShopId());
        return Result.success(page);
    }
}